<?php
require_once( '../includes/init.php');

function checkLogin() 
	{
		require_once ('class_passport.php' );
		$clsPassport = new x_Passport;
		$clsPassport->appId = 10; // 通行证服务ID
		$clsPassport->encryptKey = 'k4kfEOJdi9asUWKKSDXxCKDIfjsoUH5Aa09dnAB'; // 私钥
		/****** 登陆认证 ******/
		// 第一步：
		$clsPassport->verify ();

		// 第二步，保存 Passport 信息到当前程序的 Session
		$userSession = $clsPassport->vars;
		/*
		这里为保存 Session 代码
		*/
		if ( $clsPassport->vars['app_user_id'] > 0 )
		{
			// 若账号已激活表示已登陆当前程序
		}
		// 注：无需每次都验证 Passport，可以先通过用户在当前程序本身的 Session 判断是否已登陆
        //print_r($userSession);die();

		/****** 获取程序列表 ******/
		// $appType : 程序的类型, 系统程序为 1, 游戏为 2
		$appList = $clsPassport->appGetList ( $appType );
		$_SESSION['union_username'] = $userSession["username"];
		$_SESSION['union_nickname'] = $userSession["nickname"];
		return $_SESSION['union_username'];
	}
$siteObj=alps::newClass('mod_site','mod');

$siteInfo = $siteObj->getSiteInfo($_SERVER['HTTP_HOST']);                                                     //得到站点信息
$cookie=$siteInfo['apply_id'].'_'.'100'.'_'.$siteInfo['site_id'];
$smarty->assign('cookies',$cookie);

if (empty($siteInfo)) die('请检查域名是否错误');
require_once('include.php');
$smarty->assign('siteInfo',$siteInfo);
if ($siteInfo['site_status']==3) {
echo "<script>alert('网站审核没有通过');window.close();</script>";die;
}
$tplInfo = $siteObj->getTpl($siteInfo['template_id']);                                                         //得到模板
$tplpath=$tplInfo['temppath'];

$smarty->assign('backaddress',$_SERVER['HTTP_HOST']);


	//自动获取新服务器信息
	$serverlist = @file_get_contents("https://passport.9wee.com/payment/tools/serverlist.php");
	$serverlist = unserialize($serverlist);

	foreach($serverlist[game] as $k => $v){
		
		$show .= '
			<tr>
				<td width="161">'.$v.'</td>
				<td width="358">
		';
		if($serverlist[$k]){
		foreach($serverlist[$k] as $sk => $sv){
			if($k == 102) $sk .= ".sg.9wee.com";
			$show .= '<a class="f63" href="http://'.$sk.'?q='.$cookie.'&backurl=http://'.$_SERVER['HTTP_HOST'].'" target="_blank">'.$sv.'</a>&nbsp;&nbsp;&nbsp;&nbsp;';			
		}
		}
		$show .='
				</td>
				<td width="103"><a href="/game/?game='.$k.'" target="_blank">更多...</a></td>
			</tr>	
		';
	}
    $smarty->assign('show',$show);
    


if ($_SESSION['union_username'] =='') {
    checkLogin();
} 

if ($_SESSION['union_username'] =='') {
	$smarty->display($tplpath.'passport_unlogin.html'); 
} else {
	/**
	 * @author        刘兴龙
	 * @email         liuxing5332199@126.com
	 * @Date:         2008年5月23日
	 * @Description:  下线注册接口
	 */
	class lower_user_reg
	{
		var $key_code = 'sdfs$#%34123423434%%@@@3^$^!@#dfgf45wf33^^^44'; // 私钥
		var $postUrl = "http://u.9wee.com/index.php?module=event&action=lower_user_reg";
		function postQuery ( $postVars )
		{
			if(is_array($postVars))
			{
				foreach($postVars as $key=>$value)
				{
					$postData .= '&'.$key.'='.$value;
				}
				$postUrl = $this->postUrl.$postData;//echo $postUrl;
				$results = file_get_contents ( $postUrl );
				return $results;
			}
		}
		/**发送请求
		*@param string $apply_id 推广员ID
		*@param string $reg_time 注册时间
		*@param string $reg_ip 推广员ID
		*@param string $lower_user_account 下线账号
		*@param string $lower_user_nickname 下线昵称
		*@param string $game_id 下线所属游戏
		*@param string $upper_id 渠道ID
		*@param int money 游戏币
		*/
		function send($apply_id,$reg_time,$reg_ip,$lower_user_account,$lower_user_nickname,$game_id,$upper_id)
		{
			$game_id = 100;
			if ( is_numeric($apply_id) && strlen($apply_id) >= 8 )
			{
				$round_code = rand(1,10000);
				$string = $apply_id.$reg_time.$reg_ip.$round_code.$this->key_code.$lower_user_account.$lower_user_nickname.$game_id;
				$md5_code = md5($string);
				$postArr = array (
					'apply_id' => $apply_id,
					'reg_time' => $reg_time,
					'reg_ip'=>$reg_ip,
					'round_code' => $round_code,
					'lower_user_account' => $lower_user_account,
					'lower_user_nickname'=>$lower_user_nickname,
					'md5_code' => $md5_code,
					'game_id'=>$game_id,
					'upper_id'=>$upper_id
					);//echo "string1=".$string."<br>";
				$result = $this->postQuery ( $postArr );
			}
		}
	}

		if(!empty($_GET['q'])){
			$q_ = explode("_",$_GET['q']);			
			if($q_[3] == 1){
				$_GET['q'] = $q_[0]."_".$q_[1]."_".$q_[2];
				$q = explode("_",$_GET['q']);
				$apply_id = $q['0'];
				$game_id = $q['1'];
				$upper_id = $q['2'];
				$reg_time = time();
				$reg_ip = $_SERVER['REMOTE_ADDR'];
				$lower_user_account = $_SESSION['union_username'];
				$lower_user_nickname = $_SESSION['union_nickname'];	

				$lower_user_reg = new lower_user_reg;	
				$lower_user_reg->send($apply_id,$reg_time,$reg_ip,$lower_user_account,$lower_user_nickname,$game_id,$upper_id);
			}
		}		
		
		
    $smarty->display($tplpath.'passport_logined.html'); 
}



?>